package org.wishwork.api.inventory.upload.request;

import cn.hutool.core.util.StrUtil;
import lombok.*;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

import java.util.List;

@EqualsAndHashCode
@ToString
@Getter
public class Data {

    /**
     * 必填
     * 外部单号，会在ERP判断重复，仅可用一次
     */
    @NonNull
    private String soId;

    /**
     * 必填
     * 仓库;主仓=1，销退仓=2， 进货仓=3，次品仓 = 4
     */
    @NonNull
    private Integer warehouse;

    /**
     * 非必填
     * 备注
     */
    private String remark;

    /**
     * 必填
     * 商品明细，最大50
     */
    @NonNull
    private List<Item> items;

    private Data(String soId, Integer warehouse, String remark, List<Item> items) {
        this.soId = soId;
        this.warehouse = warehouse;
        this.remark = remark;
        this.items = items;
    }

    public static Builder buider() {
        return new Builder();
    }

    public static class Builder {
        private String soId;
        private Integer warehouse;
        private String remark;
        private List<Item> items;

        public Builder soId(String soId) {
            if (StrUtil.isEmpty(soId)) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "soId must be not null or blank");
            }
            this.soId = soId;
            return this;
        }

        public Builder warehouse(Integer warehouse) {
            if (warehouse == null) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0410, "warehouse must be not null or blank");
            }
            this.warehouse = warehouse;
            return this;
        }

        public Builder remark(String remark) {
            this.remark = remark;
            return this;
        }

        public Builder items(List<Item> items) {
            Integer maxSize = 50;
            if (items != null && items.size() > maxSize) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "商品明细，最大50");
            }
            this.items = items;
            return this;
        }

        public Data build() {
            if (StrUtil.isEmpty(this.soId)) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "soId is not set!");
            }
            if (this.warehouse == null) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "warehouse is not set!");
            }
            if (this.items == null || this.items.size() == 0) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "items is not set or items size is zero!");
            }
            return new Data(this.soId, this.warehouse, this.remark, this.items);
        }
    }


}